﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Profari_Heldur_Utan_Um_Prof
{
    public partial class CreateEssay : Form
    {

        #region Variables
        string name;
        string intro;
        int countHelpPressed = 1;

        int questionCounter = 1;
        int questionCounterMin = 2;
        int questionCounterMax = 5;
        #endregion

        #region Static variables from other forms
        string infoText = IntroductionText.introText;
        #endregion
        
        public CreateEssay()
        {
            this.StartPosition = FormStartPosition.CenterScreen;
            InitializeComponent();
        }
        
        private void CreateEssay_Load(object sender, EventArgs e)
        {
            lblHelpText.Hide();
            hideQuestions();
            lblWarningText.Hide();
        }

        #region Functions
        public void hideQuestions()
        {
            lblQuestion2.Hide();
            riQuestion2.Hide();
            lblStar2.Hide();

            lblQuestion3.Hide();
            riQuestion3.Hide();
            lblStar3.Hide();

            lblQuestion4.Hide();
            riQuestion4.Hide();
            lblStar4.Hide();

            lblQuestion5.Hide();
            riQuestion5.Hide();
            lblStar5.Hide();
        }
        #endregion

        #region Buttons
        private void btnFinish_Click(object sender, EventArgs e)
        {
            intro = richTextBox1.Text;
            name = riEssayQuestion1.Text;


            if (questionCounter == 1 && riEssayQuestion1.Text == "")
            {
                lblWarningText.Show();
                MessageBox.Show("Please fill in the textboxes marked with red star");
            }
            else if (questionCounter == 2 && riEssayQuestion1.Text == "" && riQuestion2.Text == "")
            {
                lblWarningText.Show();
                MessageBox.Show("Please fill in the textboxes marked with red star");
            }
            else if (questionCounter == 3
                && riEssayQuestion1.Text == ""
                && riQuestion2.Text == ""
                && riQuestion3.Text == "")
            {
                lblWarningText.Show();
                MessageBox.Show("Please fill in the textboxes marked with red star");
            }
            else if (questionCounter == 4
                && riEssayQuestion1.Text == ""
            && riQuestion2.Text == ""
            && riQuestion3.Text == ""
            && riQuestion4.Text == "")
            {
                lblWarningText.Show();
                MessageBox.Show("Please fill in the textboxes marked with red star");
            }
            else if (questionCounter == 5
                && riEssayQuestion1.Text == ""
            && riQuestion2.Text == ""
            && riQuestion3.Text == ""
            && riQuestion4.Text == ""
            && riQuestion5.Text == "")
            {
                lblWarningText.Show();
                MessageBox.Show("Please fill in the textboxes marked with red star");
            }
            else
            {

                System.Threading.Thread testerInterface = new System.Threading.Thread(new System.Threading.ThreadStart(OpenTesterInterface));
                testerInterface.SetApartmentState(System.Threading.ApartmentState.STA);
                testerInterface.Start();
                this.Close();
            }
        }

        //Textboxes have been made for this form so this button controls which textboxes are shown.
        private void btnAddQuestion_Click(object sender, EventArgs e)
        {

            if (questionCounter < 1)
            {
                questionCounter = questionCounterMin;
            }
            if (questionCounter > 5)
            {
                questionCounter = questionCounterMax;
            }
            if (questionCounter <= questionCounterMax)
            {
                if (questionCounter == 1)
                {
                    lblQuestion2.Show();
                    riQuestion2.Show();
                    lblStar2.Show();
                }

                if (questionCounter == 2)
                {
                    lblQuestion3.Show();
                    riQuestion3.Show();
                    lblStar3.Show();
                }

                if (questionCounter == 3)
                {
                    lblQuestion4.Show();
                    riQuestion4.Show();
                    lblStar4.Show();
                }

                if (questionCounter == 4)
                {
                    lblQuestion5.Show();
                    riQuestion5.Show();
                    lblStar5.Show();
                }
                questionCounter++;
            }

            else
            {
                MessageBox.Show("You can only create 10 questions");
            }
        }
        //Textboxes have been made for this form so this button controls which textboxes are hidden.
        private void btnRemoveLast_Click(object sender, EventArgs e)
        {
            if (questionCounter < 2)
            {
                questionCounter = questionCounterMin;
            }
            if (questionCounter > 5)
            {
                questionCounter = questionCounterMax;
            }
            if (questionCounter > 1)
            {
                if (questionCounter == 2)
                {
                    lblQuestion2.Hide();
                    riQuestion2.Hide();
                    lblStar2.Hide();
                }

                if (questionCounter == 3)
                {
                    lblQuestion3.Hide();
                    riQuestion3.Hide();
                    lblStar3.Hide();
                }

                if (questionCounter == 4)
                {
                    lblQuestion4.Hide();
                    riQuestion4.Hide();
                    lblStar4.Hide();
                }

                if (questionCounter == 5)
                {
                    lblQuestion5.Hide();
                    riQuestion5.Hide();
                    lblStar5.Hide();
                }
                --questionCounter;
            }
            else
            {
                MessageBox.Show("You have to have at least one question." +
                "Please go back if you want to choose another question form or skip it");
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {

            DialogResult backButtonProject = MessageBox.Show("When you press back the value that has been written in this form will disappear. Are you sure you want to back?", "Back", MessageBoxButtons.YesNo);
            if (backButtonProject == DialogResult.Yes)
            {
                if (CheckBoxes.chkIntroSessionValue.Equals(1))
                {
                    System.Threading.Thread introductionText = new System.Threading.Thread(new System.Threading.ThreadStart(OpenIntroText));
                    introductionText.SetApartmentState(System.Threading.ApartmentState.STA);
                    introductionText.Start();
                    this.Close();
                }
                else
                {
                    System.Threading.Thread checkBoxes = new System.Threading.Thread(new System.Threading.ThreadStart(OpenCheckBoxes));
                    checkBoxes.SetApartmentState(System.Threading.ApartmentState.STA);
                    checkBoxes.Start();
                    this.Close();
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult cancelProject = MessageBox.Show("Are you sure you want to stop making new project?", "Quit", MessageBoxButtons.YesNo);
            if (cancelProject == DialogResult.Yes)
            {
                System.Threading.Thread testerInterface = new System.Threading.Thread(new System.Threading.ThreadStart(OpenTesterInterface));
                testerInterface.SetApartmentState(System.Threading.ApartmentState.STA);
                testerInterface.Start();
                this.Close();
            }
        }

        private void btnHelp_Click(object sender, EventArgs e)
        {
            if (countHelpPressed % 2 == 1)
            {
                lblHelpText.Show();
                countHelpPressed++;
            }
            else if (countHelpPressed % 2 == 0)
            {
                lblHelpText.Hide();
                countHelpPressed++;
            }
        }
        #endregion

        //The Open forms functions makes it able to move between forms and close the first form when the second one is opened
        #region OpenForms
        public static void OpenTesterInterface()
        {
            Application.Run(new TesterInterface());
        }

        public static void OpenCheckBoxes()
        {
            Application.Run(new CheckBoxes());
        }

        public static void OpenIntroText()
        {
            Application.Run(new IntroductionText());
        }
        #endregion
        
    }
}
